home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Carnage_Contest / scripts / CC Original / tools / Sentry.lua < prev    next >
Text File  |  2010-09-16  |  10KB  |  311 lines

  1. --------------------------------------------------------------------------------
  2. -- Weapon Sentry
  3. -- Original Carnage Contest Weapon
  4. -- Script by DC, September 2010, www.UnrealSoftware.de
  5. --------------------------------------------------------------------------------
  6.  
  7. -- Setup Tables
  8. if cc==nil then cc={} end
  9. cc.sentry={}
  10. cc.sentry.sen={}
  11. cc.sentry.bullet={}
  12.  
  13. -- Load & Prepare Ressources
  14. cc.sentry.sfx_place=loadsfx("throw.ogg")                                        -- Attack Sound
  15. cc.sentry.sfx_bounce=loadsfx("bounce.wav")                                        -- Bounce
  16. cc.sentry.gfx_wpn=loadgfx("buildings/sentry.bmp")                                -- Sentry
  17. setmidhandle(cc.sentry.gfx_wpn)
  18. cc.sentry.gfx_gun=loadgfx("buildings/sentry_gun.bmp")                            -- Gun
  19. cc.sentry.gfx_icon=loadgfx("buildings/sentry_icon.bmp")                            -- Icon
  20. setmidhandle(cc.sentry.gfx_icon)
  21. cc.sentry.gfx_pro=loadgfx("weapons/shot.bmp")                                    -- Projectile Image
  22. setmidhandle(cc.sentry.gfx_pro)
  23. cc.sentry.sfx_attack=loadsfx("sentry.ogg")                                        -- Attack Sound
  24.  
  25. --------------------------------------------------------------------------------
  26. -- Weapon: Sentry
  27. --------------------------------------------------------------------------------
  28.  
  29. cc.sentry.id=addweapon("cc.sentry","Sentry",cc.sentry.gfx_icon,0,2)                -- Add Weapon (0 uses, first in round 2)
  30.  
  31. function cc.sentry.draw()                                                        -- Draw
  32.     -- HUD Positioning
  33.     if weapon_shots==0 then
  34.         hudpositioning(pos_build,cc.sentry.gfx_wpn,30,1,1,1)
  35.         -- HUD Info
  36.         hudinfo("The sentry will point into the same direction as you do!")
  37.     end
  38. end
  39.  
  40. function cc.sentry.attack(attack)                                                -- Attack
  41.     if (weapon_shots<=0) and (weapon_position==1) then
  42.         -- No more weapon switching!
  43.         useweapon(0)
  44.         playsound(cc.sentry.sfx_place)
  45.         weapon_shots=weapon_shots+1
  46.         createobject(cc.sentry.sen.id,weapon_x,weapon_y)
  47.         -- End Turn
  48.         endturn()
  49.     end
  50. end
  51.  
  52. --------------------------------------------------------------------------------
  53. -- Projectile: Bullet
  54. --------------------------------------------------------------------------------
  55.  
  56. cc.sentry.bullet.id=addprojectile("cc.sentry.bullet")            -- Add Projectile
  57.  
  58. function cc.sentry.bullet.draw(id)                                -- Draw
  59.     -- Setup draw mode
  60.     setblend(blend_light)
  61.     setalpha(1)
  62.     setcolor(255,255,0)
  63.     setscale(1,1)
  64.     -- Calculate projectile rotation
  65.     setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
  66.     -- Draw projectile
  67.     drawimage(cc.sentry.gfx_pro,projectiles[id].x,projectiles[id].y)
  68.     -- Draw Arrow if out of Screen
  69.     outofscreenarrow(projectiles[id].x,projectiles[id].y)
  70. end
  71.  
  72. function cc.sentry.bullet.update(id)                            -- Update
  73.     -- Wind + Gravity influence on speed
  74.     projectiles[id].sx=projectiles[id].sx+getwind()*0.02
  75.     projectiles[id].sy=projectiles[id].sy+getgravity()*0.75
  76.     -- Move
  77.     cc.sentry.bullet.move(id)
  78. end
  79.  
  80. function cc.sentry.bullet.move(id)
  81.     rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
  82.     -- Move (in substep loop for optimal collision precision)
  83.     msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
  84.     msubx=projectiles[id].sx/msubt
  85.     msuby=projectiles[id].sy/msubt
  86.     for i=1,msubt,1 do
  87.         projectiles[id].x=projectiles[id].x+msubx
  88.         projectiles[id].y=projectiles[id].y+msuby        
  89.         -- Collision
  90.         if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20,1,1,1,-1,projectiles[id].ignore)==1 then
  91.             -- Cause damage
  92.             if playercollision()~=0 then
  93.                 playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
  94.                 playerdamage(playercollision(),5)
  95.                 blood(projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20)
  96.             elseif objectcollision()>0 then
  97.                 objectdamage(objectcollision(),5)
  98.             end
  99.             -- Destroy terrain
  100.             for j=20,22,1 do
  101.                 terraincircle(projectiles[id].x+math.sin(math.rad(rot))*j,projectiles[id].y-math.cos(math.rad(rot))*j,3,0x00000000)
  102.             end
  103.             -- Effects
  104.             playsound(sfx_ricochet1)
  105.             particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
  106.             particlefadealpha(0.006)
  107.             particle(p_muzzle,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
  108.             -- Free projectile
  109.             freeprojectile(id)
  110.             return 1
  111.         end
  112.         -- Water
  113.         if (projectiles[id].y)>getwatery()+5 then
  114.             -- Effects
  115.             particle(p_waterhit,projectiles[id].x,projectiles[id].y)
  116.             if math.random(1,2)==1 then
  117.                 playsound(sfx_hitwater2)
  118.             else
  119.                 playsound(sfx_hitwater3)
  120.             end
  121.             -- Free projectile
  122.             freeprojectile(id)
  123.             return 1
  124.         end
  125.     end
  126. end
  127.  
  128. --------------------------------------------------------------------------------
  129. -- Object: Sentry
  130. --------------------------------------------------------------------------------
  131.  
  132. cc.sentry.sen.id=addobject("cc.sentry.sen",cc.sentry.gfx_wpn)            -- Add Object
  133.  
  134. function cc.sentry.sen.setup(id,parameter)
  135.     if objects==nil then objects={} end
  136.     objects[id]={}
  137.     objects[id].sy=0
  138.     objects[id].timer=0
  139.     objects[id].burst=0
  140.     objects[id].burstrow=0
  141.     objects[id].health=30
  142.     objects[id].dir=getplayerdirection(0)
  143.     objects[id].owner=playercurrent()
  144. end
  145.  
  146. function cc.sentry.sen.draw(id,x,y)
  147.     -- Setup draw mode
  148.     setblend(blend_alpha)
  149.     setalpha(1)
  150.     setcolor(255,255,255)
  151.     setscale(1,1)
  152.     setrotation(0)
  153.     -- Draw Sentry
  154.     drawimage(cc.sentry.gfx_wpn,x,y)
  155.     -- Draw Gun
  156.     setscale(objects[id].dir,1)
  157.     if objects[id].dir==1 then
  158.         drawimage(cc.sentry.gfx_gun,x+4,y-6)
  159.     else
  160.         drawimage(cc.sentry.gfx_gun,x-3,y-6)
  161.     end
  162. end
  163.  
  164. function cc.sentry.sen.update(id,x,y)
  165.     -- Gravity influence on speed
  166.     objects[id].sy=objects[id].sy+getgravity()
  167.     -- Move (in substep loop for optimal collision precision)
  168.     msubt=math.ceil(math.abs(objects[id].sy)/15)
  169.     msuby=objects[id].sy/msubt
  170.     for i=1,msubt,1 do
  171.         y=y+msuby
  172.         -- Collision
  173.         if collision(cc.sentry.gfx_wpn,x,y,1,1,1,-1,id)==1 then
  174.             if (math.abs(objects[id].sy)>0.5) then playsound(cc.sentry.sfx_bounce) end
  175.             y=y-msuby
  176.             objects[id].sy=-objects[id].sy*0.3
  177.             msuby=-msuby*0.3
  178.         end
  179.         -- Water
  180.         if y>getwatery()+5 then
  181.             -- Effects
  182.             particle(p_waterhit,x,y)
  183.             playsound(sfx_hitwater1)
  184.             -- Remove
  185.             removeobject(id)
  186.             return
  187.         end
  188.     end
  189.     objectposition(id,x,y)
  190.     -- Scan for Enemies / Shoot
  191.     if objects[id].burst==0 then
  192.         objects[id].timer=objects[id].timer+1
  193.         if objects[id].timer>=50 then
  194.             -- Scan Blink
  195.             particle(p_muzzle,x,y-6)
  196.             local team=getplayerteam(objects[id].owner)
  197.             if team==0 then particlecolor(0,255,0) end
  198.             if team==1 then particlecolor(255,255,0) end
  199.             if team==2 then particlecolor(255,0,0) end
  200.             if team==3 then particlecolor(80,200,255) end
  201.             if team==4 then particlecolor(180,60,255) end
  202.             if team==5 then particlecolor(255,180,0) end
  203.             if team==6 then particlecolor(0,180,0) end
  204.             if team==7 then particlecolor(50,100,255) end
  205.             particlesize(0.25,0.25)
  206.             particlealpha(1.0)
  207.             particlefadealpha(0.05)
  208.             -- Scan all players
  209.             objects[id].timer=0
  210.             local players=playertable(0,0)
  211.             local ownally=getplayeralliance(objects[id].owner)
  212.             for i=1,#players do
  213.                 -- Enemy?
  214.                 if getplayeralliance(players[i])~=ownally then
  215.                     -- In Range?
  216.                     if math.abs(getplayery(players[i])-y)<30 then
  217.                         if math.abs(getplayerx(players[i])-x)<400 then
  218.                             -- Righ Direction?
  219.                             if (objects[id].dir==1 and getplayerx(players[i])>x) or (objects[id].dir==-1 and getplayerx(players[i])<x) then
  220.                                 -- Something blocking?
  221.                                 local dist=(math.abs(getplayerx(players[i])-x))-8
  222.                                 local tx=x+objects[id].dir*14
  223.                                 local ty=y-3
  224.                                 local ok=1
  225.                                 for offsetx=1,dist,5 do
  226.                                     if collision(col3x3,tx+offsetx*objects[id].dir,ty,1,1,1,players[i])==1 then
  227.                                         ok=0; break;
  228.                                     end
  229.                                 end
  230.                                 if ok==1 then
  231.                                     -- Ready to fire!
  232.                                     objects[id].burst=1
  233.                                     objects[id].burstrow=objects[id].burstrow+1
  234.                                     if objects[id].burstrow>=6 then
  235.                                         -- Overheating! (don't shoot for 10 seconds after 5 bursts)
  236.                                         objects[id].burst=0
  237.                                         objects[id].timer=(-50)*10
  238.                                         playsound(sfx_steam)
  239.                                         particle(p_smoke,x,y-6); particle(p_smoke,x,y-10); particle(p_smoke,x-3,y-8); particle(p_smoke,x+3,y-8)
  240.                                         particle(p_spark,x,y-9); particle(p_spark,x-4,y-9); particle(p_spark,x+4,y-9)
  241.                                     end
  242.                                 end
  243.                             end
  244.                         end
  245.                     end
  246.                 end
  247.             end
  248.             if objects[id].burst==0 then
  249.                 objects[id].burstrow=0
  250.             end
  251.         end
  252.     else
  253.         if getframe()%5==0 then
  254.             objects[id].burst=objects[id].burst+1
  255.             if objects[id].burst>10 then
  256.                 -- Burst Done!
  257.                 objects[id].burst=0
  258.                 objects[id].timer=0
  259.             else
  260.                 -- Shoot!
  261.                 playsound(cc.sentry.sfx_attack)
  262.                 local pid=createprojectile(cc.sentry.bullet.id)
  263.                 projectiles[pid]={}
  264.                 -- Ignore Object
  265.                 projectiles[pid].ignore=id
  266.                 -- Set initial position of projectile
  267.                 projectiles[pid].x=x+objects[id].dir*14
  268.                 projectiles[pid].y=y-3
  269.                 -- Set speed of projectile
  270.                 math.randomseed(objects[id].burst*12345)
  271.                 offset=-6.0+math.random()*12.0
  272.                 projectiles[pid].sx=math.sin(math.rad(90*objects[id].dir+offset))*15.0
  273.                 projectiles[pid].sy=-math.cos(math.rad(90*objects[id].dir+offset))*15.0
  274.                 -- Initial movement
  275.                 projectiles[pid].x=projectiles[pid].x-objects[id].dir*20
  276.                 cc.sentry.bullet.move(pid)
  277.                 -- Effects
  278.                 particle(p_muzzle,projectiles[pid].x,projectiles[pid].y)
  279.                 particle(p_smoke,projectiles[pid].x,projectiles[pid].y)
  280.                 particlespeed(-0.2+math.random()*0.4+getwind()*10.0,-1.0+math.random()*0.6)
  281.                 particlefadealpha(0.005)
  282.                 particle(p_bullet,x,y-7)
  283.             end
  284.         end
  285.         return 1
  286.     end
  287. end
  288.  
  289. function cc.sentry.sen.damage(id,damage)
  290.     if objects[id].health>0 then
  291.         objects[id].health=objects[id].health-damage
  292.         if objects[id].health<=0 then
  293.             local x=getobjectx(id)
  294.             local y=getobjecty(id)
  295.             -- Destroy terrain
  296.             terrainexplosion(x,y,35,1)
  297.             -- Crater
  298.             grey=math.random(0,40)
  299.             if math.random(0,1)==1 then
  300.                 terrainalphaimage(gfx_crater100,x,y,math.random(6,9)*0.1,grey,grey,grey)
  301.             else
  302.                 terrainalphaimage(gfx_crater125,x,y,math.random(6,9)*0.1,grey,grey,grey)
  303.             end
  304.             -- Remove
  305.             removeobject(id)
  306.             -- Cause damage
  307.             arealdamage(x,y,65,35)
  308.         end
  309.     end
  310. end
  311.